package com.ljh.mall.lnterceptor;

import com.alibaba.fastjson.JSON;
import com.ljh.common.constant.AuthConstant;
import com.ljh.common.vo.MemberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * @author ：刘剑鸿
 * @date ：Created in 2023/4/15 15:06
 * @description：
 * @modified By：
 * @version: $
 */
public class AuthInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberVO> threadLocal = new ThreadLocal();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //System.out.println("这是session"+ JSON.toJSONString(session));
        //Object session1 = redisTemplate.opsForValue().get("session");
        //System.out.println("这是session1---"+session);
        Object attribute = session.getAttribute(AuthConstant.AUTH_SESSION_REDIS);
        //System.out.println("这是attribute"+attribute);
        if (attribute != null){

            MemberVO memberVO = (MemberVO)attribute;
            threadLocal.set(memberVO);
            return true;
        }
       // System.out.println("这是登录"+attribute);

        session.setAttribute(AuthConstant.AUTH_SESSION_MSG,"请先登录");
       //return "redirect:http://localhost:30000/login.html";

        response.sendRedirect("http://localhost:30000/login.html");
        return false;
    }
}
